﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Tema3.Models;

namespace Tema3.Services
{
    public class TradesInstance
    {
        private static TradesInstance instance = null;
        private static object locker = new object();
        private Dictionary<string, BitcointClient> dictTrades;

        public static TradesInstance Instance
        {
            get
            {
                if (instance == null)
                {
                    lock(locker)
                    {
                        if(instance == null)
                        {
                            instance = new TradesInstance();
                        }
                    }
                }

                return instance;
            }
        }

        private TradesInstance()
        {
            dictTrades = new Dictionary<string, BitcointClient>();
        }

        public TradeResponse AddNewTradeToClient(string connectionId, BitcoinTrade bitcoinTrade)
        {
            BitcointClient client;
            if(!dictTrades.ContainsKey(connectionId))
            {
                client = new BitcointClient(connectionId);
                dictTrades.Add(connectionId, client);
            }
            else
            {
                client = dictTrades[connectionId];
            }

            return client.AddTrade(bitcoinTrade);
        }

        public TradeResponse RemoveTradeFromClient(string connectionId, int tradeId)
        {

            if (!dictTrades.ContainsKey(connectionId))
            {
                return new TradeResponse("The current client " + connectionId + " has no trades.");   
            }

            BitcointClient client = dictTrades[connectionId];
            TradeResponse response = client.RemoveTrade(tradeId);
            return response;
        }

        public TradeResponse ClearStoredInfo(string connectionId)
        {
            if (!dictTrades.ContainsKey(connectionId))
            {
                return new TradeResponse("The current client " + connectionId + " has no trades.");
            }
            else
            {
                BitcointClient client = dictTrades[connectionId];
                TradeResponse response = client.Clear();
                return response;
            }
        }

        public string RemoveClient(string connectionId)
        {
            if (!dictTrades.ContainsKey(connectionId))
            {
                return "The client " + connectionId + " is not connected.";
            }

            dictTrades.Remove(connectionId);

            return "The client " + connectionId + " is disconnected!";
        }

        public string AddNewClient(string connectionId)
        {
            if (dictTrades.ContainsKey(connectionId))
            {
                return "The client " + connectionId + " is already connected.";
            }

            dictTrades.Add(connectionId, new BitcointClient(connectionId));

            return "The client " + connectionId + " is connected!";
        }
    }
}